<?php

include_once './utils/conn.php';
header("Access-Control-Allow-Origin: *");
//保存图书入口
if (isset($_POST["action"]) && $_POST["action"] == "add") {
    $book = [
        "bookName" => $_POST["book_name"],
        "author" => $_POST["author"],
        "publish" => $_POST["publish"],
        "price" => $_POST["price"],
        "content" => $_POST["content"],
        "cover" => $_POST["cover"]
    ];
    add($conn, $book);
}
//删除图书入口
if (isset($_GET["action"]) && $_GET["action"] == "del") {
    $bookId = $_GET["book_id"];
    del($conn, $bookId);
}
//获取所有图书入口
if (isset($_GET["action"]) && $_GET["action"] == "getAll") {

    getAllBooks($conn);
}
//更新图书入口
if (isset($_POST["action"]) && $_POST["action"] == "update") {
    $book = [
        "bookId" => $_POST["book_id"],
        "bookName" => $_POST["book_name"],
        "author" => $_POST["author"],
        "publish" => $_POST["publish"],
        "price" => $_POST["price"],
        "content" => $_POST["content"],
        "cover" => $_POST["cover"]
    ];
    update($conn, $book);
}
//查询图书入口
if (isset($_POST["action"]) && $_POST["action"] == "query") {
    $query = $_POST["query"];
    search($conn, $query);
}

// 保存图书
function add($conn, $book)
{
    //写sql语句
    $sql = "insert into books(book_name,author,publish,price,content,cover) values(?,?,?,?,?,?)";
    //创建预处理语句
    $stmt = mysqli_prepare($conn, $sql);
    //对占位符进行赋值
    mysqli_stmt_bind_param(
        $stmt,
        "sssdss",
        $book["bookName"],
        $book["author"],
        $book["publish"],
        $book["price"],
        $book["content"],
        $book["cover"]
    );
    //执行预处理语句,成功true,失败,false
    $result = mysqli_stmt_execute($stmt);
    //根据执行结果,分别输出,
    if ($result) {
        //成功
        echo json_encode(["code" => 1, "msg" => "数据保存成功!"]);
    } else {
        //失败
        echo json_encode(["code" => -1, "msg" => "数据保存失败!"]);
    }
}

//删除图书
function del($conn, $book_id)
{
    //编写sql语句
    $sql = "delete from books where book_id=?";
    //执行预处理语句
    $stmt = mysqli_prepare($conn, $sql);
    //对占位符进行赋值
    mysqli_stmt_bind_param($stmt, "i", $book_id);
    //执行查询
    $result = mysqli_stmt_execute($stmt);
    //根据执行结果,分别输出,
    if ($result) {
        //成功
        echo json_encode(["code" => 1, "msg" => "数据删除成功!"]);
    } else {
        //失败
        echo json_encode(["code" => -1, "msg" => "数据删除失败!"]);
    }
}

//更新图书
function update($conn, $book)
{
    //图书的更新是根据book_id进行更新的
    $sql = "update books set book_name=?,author=?,publish=?,price=?,content=?,cover=? where book_id=?";
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param(
        $stmt,
        "sssdssi",
        $book["bookName"],
        $book["author"],
        $book["publish"],
        $book["price"],
        $book["content"],
        $book["cover"],
        $book["bookId"]
    );
    $result = mysqli_stmt_execute($stmt);
    //根据执行结果,分别输出,
    if ($result) {
        //成功
        echo json_encode(["code" => 1, "msg" => "数据更新成功!"]);
    } else {
        //失败
        echo json_encode(["code" => -1, "msg" => "数据更新失败!"]);
    }
}

//查询图书
function search($conn, $query)
{
    //使用占位符进行sql的编写
    $sql = "select * from books where book_id= ? || book_name  like ? || author like ? || publish like ? || content like ?";
    $stmt = mysqli_prepare($conn, $sql);
    //对占位符进行赋值
    $like_query = "%{$query}%";
    mysqli_stmt_bind_param($stmt, "issss", $query, $like_query, $like_query, $like_query, $like_query);
    //执行预处理语句,如果成功,true,失败false
    if (mysqli_stmt_execute($stmt)) {
        $result = $stmt->get_result();
        while ($row = mysqli_fetch_array($result)) {
            $data[] = [
                "book_id" => $row[0],
                "book_name" => $row[1],
                "author" => $row[2],
                "publish" => $row[3],
                "price" => $row[4],
                "content" => $row[5],
                "cover" => $row[6]
            ];
        }
        echo json_encode(["code" => 1, "msg" => "查询成功", "num" => count($data), "data" => $data], JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(["code" => 0, "msg" => "查询成功", "num" => 0, "data" => null], JSON_UNESCAPED_UNICODE);
    }
}

//查询图书
function getAllBooks($conn)
{
    //使用占位符进行sql的编写
    $sql = "select * from books ";
    $result = mysqli_query($conn, $sql);
    while ($row = mysqli_fetch_array($result)) {
        $data[] = [
            "book_id" => $row[0],
            "book_name" => $row[1],
            "author" => $row[2],
            "publish" => $row[3],
            "price" => $row[4],
            "content" => $row[5],
            "cover" => $row[6]
        ];
    }
    echo json_encode(["code" => 1, "msg" => "查询成功", "num" => count($data), "data" => $data], JSON_UNESCAPED_UNICODE);
}
